//map地图
var map, geolocation;
function mapFunction(){
	//加载地图，调用浏览器定位服务
	map = new AMap.Map('container', {
		resizeEnable: true,
		zoom: 18
	});	
			
	map.plugin('AMap.Geolocation', function() {
		var geolocation = new AMap.Geolocation({
			enableHighAccuracy: true,//是否使用高精度定位，默认:true
			timeout: 10000,          //超过10秒后停止定位，默认：无穷大
			buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
			zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
			buttonPosition:'RB'
		});
		map.addControl(geolocation);
		geolocation.getCurrentPosition();
		
		AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
		//alert("12");
		
		AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
	});
}	

//解析定位结果
function onComplete(data) {
	var str=['定位成功'];
	str.push('经度：' + data.position.getLng());
	str.push('纬度：' + data.position.getLat());
	str.push('精度：' + data.accuracy + ' 米');
	str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
	Lat=data.position.getLat();
	Lng=data.position.getLng();
	//alert(Lat);
	
	
	//逆地理编码 坐标——地址
	var lnglatXY=[Lng, Lat];//地图上所标点的坐标
	AMap.plugin('AMap.Geocoder',function(){
		var geocoder = new AMap.Geocoder({
			//city: "010"//城市，默认：“全国”
		});
		geocoder.getAddress(lnglatXY,function(status,result){
			if(status=='complete'){
				address= result.regeocode.formattedAddress;
				$(".adr_st").text(address);
				Address=address;
				getAjax_distance(Lat,Lng,address);
			}
		});	
	});
}
	
//解析定位错误信息
function onError(data) {
	document.getElementById('address').innerHTML = '定位失败';
}


//ajax请求数据ditance
function getAjax_distance(Lat,Lng,address){
	var aj = $.ajax( {  
		url:'../backend/sign!getShopDistance',// 跳转到 action  
		data:{  
			lat:Lat,  
			lng:Lng 
		},  
		type:'post',  
		cache:false,  
		dataType:'json',  
		success:function(data) {  
			//var obj=eval(data);
			//alert(typeof(data));
			//alert(data.distance+':'+data.shop);
			distance=data.distance;
			var shop = data.shop;
			if(distance<500){
				$(".weui_dialog_title").text(shop);

			}else{

				$(".weui_dialog_title").text("地点异常");
				$(".weui_dialog_title").css("color","#f00");

			}
			
		},  
		error : function() {  
			// view("异常！");  
			//alert("异常distance");  
			alert("网络请求失败，请重试！");
		}  
	});
}

//请求保存打卡信息

function getAjax_savemessage(){
	//alert('Lat:'+Lat+'Lng:'+Lng+'mytime:'+mytime);
	//alert(code);
	

	var aj = $.ajax( {  
		url:'../backend/Sign!addSignRecord',// 跳转到 action  
		data:{ 
			lat:Lat,  
			lon:Lng ,
			sign_time:time_day,
			sign_address:Address,
			sign_date:time_year,
			distance:distance,
			photos:mediaId,
			memo:remark
		},  
		type:'post',  
		cache:false,  
		dataType:'json',  
		success:function(data) {  
			//var obj=eval(data);
			$("#toast").css("display","block");
			setTimeout(function () {
				$("#toast").css("display","none");
			}, 2000);
			//console.log(data);
		},  
		error : function() {  
			// view("异常！");  
			//alert("异常savemessage"); 
			alert("网络请求失败，请重试！"); 
		}  
	});
}

//存code
function getAjax_savecode(){
	//alert('Lat:'+Lat+'Lng:'+Lng+'mytime:'+mytime);	
	var code = getQueryString("code");//传给后台编码
	var existedCode = window["code"]||"";
	if(code == existedCode){
		return;
	}
	window["code"]=code;
	//alert(code);
	var aj = $.ajax( {  
		url:'../backend/authen!wxLogin',// 跳转到 action  
		data:{ 
			
			code:code
			
		},  
		type:'post',  
		cache:false,  
		dataType:'json',  
		success:function(data) {  
			//var obj=eval(data);
			/*alert(data);
			var str = JSON.stringify(data); 
			alert(str);*/
			//打卡dialog
			$("#sign").removeClass("grey");

			$("#sign").click(function(){
				$(".weui_dialog_confirm").css("display","block");	
				time_day = new Date().Format("yyyy-MM-dd HH:mm:ss");//转换年月日时间格式
				time_year = time_day.substr(0,10); //获取年月日
				mytime = time_day.substr(11,8); //获取时间
				//alert(time1);
				$(".dialog_time").text(mytime);
				$(".dialog_address").text(Address);
			});

			signature_ajax();
		},  
		error : function(error) {  
			// view("异常！");  
			//alert(error);  
			alert("网络请求失败，请重试！");
		}  
	});
}


//解析URL参数
function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
} 
		
	// 日期格式化	
	Date.prototype.Format = function(formatStr)   
	{   
		var str = formatStr;   
		var M;
		var Week = ['日','一','二','三','四','五','六'];  
		  
		str=str.replace(/yyyy|YYYY/,this.getFullYear());   
		str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); 
			
		M = this.getMonth()+1,//月份 
		str=str.replace(/MM/,M>9?M.toString():'0' + M); 
		str=str.replace(/M/g,this.getMonth());   
		  
		str=str.replace(/w|W/g,Week[this.getDay()]);   
		  
		str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());   
		str=str.replace(/d|D/g,this.getDate());   
		  
		str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());   
		str=str.replace(/h|H/g,this.getHours());   
		str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());   
		str=str.replace(/m/g,this.getMinutes());   
		  
		str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());   
		str=str.replace(/s|S/g,this.getSeconds());   
		  
		return str;   
	}

	//显示时间
	function showTime(){
        var lab = document.getElementById("time_now");   //这是获取显示时间的label
        var date = new Date();
           
        var hour = date.getHours();
        	if(hour < 10)
            	hour = "0"+hour;
           	var min = date.getMinutes();
            if(min < 10)
                min = "0"+min;
            var sec = date.getSeconds();
            if(sec < 10)
                sec = "0"+sec;
            lab.innerHTML = hour+":"+min;
            setTimeout('showTime()',1000);                                  //页面自动调用函数，每隔1000ms调用showTime()函数
    }